#
#
#
#

require 'ph/ui/page'
require 'fileutils'

module Ph

	class Installer

		def initialize(application = $application, install_path=nil)
			@application  = application
			@install_path = (install_path ? install_path : application.path)
		end

		def install_pages()

			Dir.new('page').each do |filename|
				if File.extname(filename) ==  '.rb' then
					name = File.basename(filename, '.rb')
					classname = @application.prefix + name.capitalize + 'Page'
					content = File.new('page/' + filename, 'r').read

					@application.log "Installing #{filename} (#{classname})."

					code =  %{
						class #{classname} < Ph::Page
							
							def setup
								#{content}
							end

						end

						#{classname}.new(@application, name).saveAs("#{@install_path}/#{name}.html")
					}

					eval(code) rescue self.error()
				end
			end

		end

		def install()
			
			@application.logger.log "Installing #{@application.title} to #{@install_path}."

			self.install_pages
			self.install_resources('resources/img')
			self.install_resources('resources/css')
						
		end

		def error()
			@application.log $!.message + $!.backtrace.join("\n")
		end

	end

end
